layui.use(['form','layer','laydate','table','util'],function (){
    let form = layui.form,layer = layui.layer,table = layui.table,util = layui.util;

    //监听 table 头部工具栏，行内工具栏 tool(fistDataTable)
    table.on('toolbar(fistDataTable)',function (obj) {
        let layEvent = obj.event;
        //新增数据
        if (valueIsEqual(layEvent,'add')){
            top.layer.open({
                title:'新增用户信息',
                type:2,
                resize:false,
                area:['550px','720px'],
                content: '/systemDefend/userAdmin/addUserInfo',
                end:function (obj) {
                    selectUserInfo();
                }
            });
        }else if (valueIsEqual(layEvent,'update')){
            //修改数据
            //获取选中行的数据
            let utData = layui.table.checkStatus('userTable').data;
            if (utData.length > 1){
                layer.msg("每次只能修改一条数据",{icon:3,time:1500});
                return;
            }
            if (utData.length < 1){
                layer.msg("请选择一条数据",{icon:3,time:1500});
                return;
            }
            //全局变量传值，使子页面可以获取
            setValue("userTabTable",utData[0]);
            parent.layer.open({
                title:'修改用户信息',
                type:2,
                resize:false,
                area:['550px','720px'],
                content: '/systemDefend/userAdmin/updateUserInfo',
                end:function (obj) {
                    selectUserInfo();
                }
            });
        }else if (valueIsEqual(layEvent,'delete')){
            //删除数据
            //获取选中行的数据
            let delData = layui.table.checkStatus('userTable').data;
            if (delData.length > 1){
                layer.msg("每次只能删除一条数据",{icon:3,time:1500});
                return;
            }
            if (delData.length < 1){
                layer.msg("请选择一条数据",{icon:3,time:1500});
                return;
            }
            setValue("delUrId",delData[0].urId)
            top.layer.confirm('请问您真的要删除该用户信息吗？',{icon:0},function (index) {
                sendAjax("/systemDefend/userAdmin/deleteUserInfo",{"urId":getValue("delUrId")},function (result){
                    if(valueIsEqual(result.code,"00")){
                        top.layer.close(index);
                        selectUserInfo();
                    }else if (valueIsEqual(result.close,"02")){
                        layer.msg('删除失败',{icon:65,time:1500,title:'删除提示'});
                    }
                });
            });
        }
    });

    //监听行内工具栏
    table.on('tool(fistDataTable)',function (obj) {
        //监听事件
        let layEvent = obj.event;
        if (valueIsEqual(layEvent,'perm')){
            setValue("urNm",obj.data.urNm);
            parent.layer.open({
                title:'查看用户权限信息',
                type:2,
                resize:false,
                area:['500px','450px'],
                content: '/systemDefend/userAdmin/showUserPermInfo',
                end:function (obj) {
                    selectUserInfo();
                }
            });
        }
    })
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 初始化生成table数据
*/
$(function () {
    selectUserInfo();
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 生成table数据
*/
function selectUserInfo() {
    //部门编号
    let urNm= $('#urNm').val();
    //部门名称
    let urRealNm = $('#urRealNm').val();
    //统一的分页请求类型
    let urJsonData = {
        //统一分页请求
        "qryReqFir" : urNm,
        "qryReqSeco" : urRealNm
    }
    //时间戳
    let tableTitle = '用户信息表'+layui.util.toDateString(new Date(),'yyyyMMddHHmmss');
    layui.table.render({
        elem:'#dataTable',
        id:'userTable',
        url:'/systemDefend/userAdmin/queryUserList',
        parseData:function (result) {
            return{
                "code":result.code,
                "msg":result.msg,
                "count":result.data.pageCount,
                "data":result.data.list
            }
        },
        size:'sm',
        //分页的起止页号，每页条数
        page:{
            curr:1,
            limit:15,
            limits:[15,30,50,100]
        },
        //自定义的页码参数名称、每页条数参数名称
        request:{
            pageName:'pageNum',
            limitName:'pageSize'
        },
        //table的高度
        height:550,
        //查询条件，json格式
        where:urJsonData,
        cols:[[
            {type:'checkbox'},
            {type:'numbers',title:'序号',align:'center'},
            {field:'urNm',title:'账号'},
            {field:'urRealNm',title:'姓名'},
            {field:'urAge',title:'年龄',align:'center'},
            {field:'urSex',title:'性别',align:'center'},
            {field:'urAds',title:'地址'},
            {field:'urTel',title:'电话'},
            {field:'urDwCode',title:'单位代码'},
            {field:'urBmCode',title:'部门代码'},
            {field:'roleInfoVO',title:'角色',templet:function (d) {
                    return d.roleInfoVO.roDesc;
                }},
            {field:'菜单权限',title:'菜单权限',toolbar:'#trToolBar',align: 'center'},
            {field:'urPhotoPath',title:'头像路径'},
            {field:'urDelTp',title:'启用',align: 'center',templet:function (d) {
                    if (valueIsEqual(d.urDelTp,'0')){
                        return "启用";
                    }
                    return "禁用";
                }},
            {field:'stDt',title:'创建日期',templet:function (d) {
                    return layui.util.toDateString(d.stDt,'yyyy-MM-dd');
                }},
            {field:'utDt',title:'修改日期',templet:function (d) {
                    return layui.util.toDateString(d.utDt,'yyyy-MM-dd');
                }},
            {field:'urId',title:'用户ID',hide:true},
        ]],
        //头部工具条
        toolbar: '#totalToolBar',
        //导出时候excel文件标题
        title:tableTitle
    });
}